package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gillion.grule.client.utils.Streams;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.rules.admin.dao.DecisionTreeDao;
import com.rules.admin.entity.DecisionTree;
import com.rules.admin.service.DecisionTreeService;
import com.rules.admin.service.RulePackageService;
import com.rules.admin.service.RuleVariableService;
import com.rules.admin.service.TreeNodeService;
import com.rules.admin.service.TreeDedutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;

@Service
public class DecisionTreeServiceImpl extends ServiceImpl<DecisionTreeDao, DecisionTree> implements DecisionTreeService {

   @Autowired
   private RulePackageService rulePackageService;
   @Autowired
   private TreeNodeService treeNodeService;
   @Autowired
   private TreeDedutionService treeDedutionService;
   @Autowired
   private RuleVariableService ruleVariableService;


   public List<DecisionTree> getDecisionTreesByPackageId(long packageId) {

      LambdaQueryWrapper<DecisionTree> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DecisionTree::getRulePackageId,packageId);
      queryWrapper.orderByDesc(DecisionTree::getPriority);
      queryWrapper.orderByAsc(DecisionTree::getCreateTime);
      return this.baseMapper.selectList(queryWrapper);

   }

   public List<DecisionTree> getDecisionTreesByPackageIdAndStatus(long packageId) {
      LambdaQueryWrapper<DecisionTree> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DecisionTree::getRulePackageId,packageId);
      queryWrapper.eq(DecisionTree::getTreeStatus,0);
      queryWrapper.orderByDesc(DecisionTree::getPriority);
      queryWrapper.orderByAsc(DecisionTree::getCreateTime);
      return this.baseMapper.selectList(queryWrapper);

   }

   public List<Map<String, Object>> loadDecisionTrees(Long rulePacageId) {
      List<DecisionTree> tables = this.getDecisionTreesByPackageId(rulePacageId);
      return Streams.of(tables).map(new Function<DecisionTree, Map<String, Object>>() {
         public Map<String, Object> apply(DecisionTree input) {
            Map<String, Object> table = Maps.newHashMap();
            table.put("id", input.getId() + "");
            table.put("name", input.getName());
            table.put("priority", input.getPriority());
            table.put("type", 2);
            return table;
         }
      }).collectToList();
   }

   public void create(DecisionTree decisionTree) {
      decisionTree.setPriority(this.rulePackageService.getPriority(decisionTree.getRulePackageId()));
      this.save(decisionTree);
   }

   public void deleteDecisionTree(Long id) {
      this.treeNodeService.deleteByTreeId(id);
      this.treeDedutionService.deleteByTreeId(id);
      this.ruleVariableService.deleteVariables(5, id);
      this.removeById(id);
   }
}
